package mt.client;



import mt.Order;
import mt.comm.ClientComm;
import mt.dummy.ClientCommDummy;
import mt.dummy.ClientSideMessageDummy;
import mt.interfaceGui.GUI;

public class MicroTraderClientDummy implements MicroTraderClient {
	
	private ClientCommDummy clientComm;
	
	

	@Override
	public void start(ClientComm clientComm) {
		GUI gui = new GUI(clientComm);
		gui.execute();
		
		this.clientComm = (ClientCommDummy) clientComm;
		while (true) {
			
				ClientSideMessageDummy message = (ClientSideMessageDummy) clientComm.getNextMessage();
				treatOrder(message.getOrder());
		}
	}

	private void treatOrder(Order order) {
		String stock = order.getStock();
		int numberOfUnits = order.getNumberOfUnits();
		double pricePerUnit = order.getPricePerUnit();
		
		if (order.isBuyOrder()) {
			clientComm.getBuyingOrders().add(order);
			clientComm.addOrderToTableModel(order);
			
			for (Order entry : clientComm.getSellingOrders()) {
				if (entry.getStock().equals(stock) && entry.getPricePerUnit() == pricePerUnit) {
					
					if (entry.getNumberOfUnits() == numberOfUnits) {
						
						clientComm.deleteOrderFromTableModel(entry);
						clientComm.deleteOrderFromTableModel(order);
						clientComm.addOrderToHist(entry);
						clientComm.addOrderToHist(order);
						clientComm.getSellingOrders().remove(entry);
						clientComm.getBuyingOrders().remove(order);
						
						
					} else if (entry.getNumberOfUnits() > numberOfUnits) {
						int old= entry.getNumberOfUnits();
						entry.setNumberOfUnits(numberOfUnits);
						clientComm.addOrderToHist(entry);
						entry.setNumberOfUnits(old);
						entry.setNumberOfUnits(entry.getNumberOfUnits()-numberOfUnits);
						clientComm.deleteOrderFromTableModel(order);
						clientComm.getBuyingOrders().remove(order);
						clientComm.addOrderToHist(order);
						clientComm.UpdateOrderOnTableModel(entry);
				
						
					} else {
						int old= order.getNumberOfUnits();
						order.setNumberOfUnits(entry.getNumberOfUnits());
						clientComm.addOrderToHist(order);
						order.setNumberOfUnits(old);
						order.setNumberOfUnits(order.getNumberOfUnits()-entry.getNumberOfUnits());
						clientComm.getSellingOrders().remove(entry);
						clientComm.deleteOrderFromTableModel(entry);
						clientComm.addOrderToHist(entry);
						clientComm.UpdateOrderOnTableModel(order);
						
					
					}
				}
			}
			
		}
		
		if(order.isSellOrder()) {
			clientComm.getSellingOrders().add(order);
			clientComm.addOrderToTableModel(order);
			for (Order entry : clientComm.getBuyingOrders()) {
				if (entry.getStock().equals(stock) && entry.getPricePerUnit() >= pricePerUnit) {
					
					if (entry.getNumberOfUnits() == numberOfUnits) {
						clientComm.deleteOrderFromTableModel(entry);
						clientComm.deleteOrderFromTableModel(order);
						clientComm.addOrderToHist(entry);
						clientComm.addOrderToHist(order);
						clientComm.getBuyingOrders().remove(entry);
						clientComm.getSellingOrders().remove(order);
					}
					else if (entry.getNumberOfUnits() > numberOfUnits) {
						int old= entry.getNumberOfUnits();
						entry.setNumberOfUnits(numberOfUnits);
						clientComm.addOrderToHist(entry);
						entry.setNumberOfUnits(old);
						entry.setNumberOfUnits(entry.getNumberOfUnits()-numberOfUnits);
						clientComm.deleteOrderFromTableModel(order);
						clientComm.getSellingOrders().remove(order);
						clientComm.addOrderToHist(order);
						clientComm.UpdateOrderOnTableModel(entry);
						
					
						}
					else {
						int old= order.getNumberOfUnits();
						order.setNumberOfUnits(entry.getNumberOfUnits());
						clientComm.addOrderToHist(order);
						order.setNumberOfUnits(old);
						order.setNumberOfUnits(order.getNumberOfUnits()-entry.getNumberOfUnits());
						clientComm.getBuyingOrders().remove(entry);
						clientComm.deleteOrderFromTableModel(entry);
						clientComm.addOrderToHist(entry);
						clientComm.UpdateOrderOnTableModel(order);
					}
				}
			}
			
		}
	}

}
